package is.web.htgen.form;

import is.bean.entity.department.Department;
import is.bean.entity.department.DepartmentBeanRemoteHome;
import is.bean.entity.employee.Employee;
import is.bean.entity.employee.EmployeeBeanRemote;
import is.web.htgen.Locale;
import java.rmi.RemoteException;
import javax.ejb.FinderException;
import nullutil.NullUtil;

/**
 * CRUD form generator
 *
 * @author aspr0
 */
public class EmployeeForm extends AbstractForm
{
    private final String rowsText =
            "<tr>"
            + "<td>Id</td>"
            + "<td>%s</td>"
            + "</tr>"
            + "<tr>"
            + "<td>Last name</td>"
            + "<td>%s</td>"
            + "</tr>"
            + "<tr>"
            + "<td>First name</td>"
            + "<td>%s</td>"
            + "</tr>"
            + "<tr>"
            + "<td>Middle name</td>"
            + "<td>%s</td>"
            + "</tr>";
    private final String rowsSelect =
            "<tr>"
            + "<tr>"
            + "<td>Department</td>"
            + "<td>%s</td>"
            + "</tr>";
    protected EmployeeBeanRemote bean;
    private DepartmentBeanRemoteHome departmentHome;
    private Select select = new Select();

    public void setBean(EmployeeBeanRemote bean)
    {
        this.bean = bean;
    }

    public void setDepartmentHome(DepartmentBeanRemoteHome departmentHome)
    {
        this.departmentHome = departmentHome;
    }

    public void setSelect(Select select)
    {
        this.select = select;
    }

    public String genCopyUpdate(boolean isUpdate) throws RemoteException, FinderException
    {
        Employee employee = bean.getData();

        Integer id = employee.getId();
        String inputLastName = String.format(inputText, "lastName", employee.getLastName());
        String inputFirstName = String.format(inputText, "firstName", employee.getFirstName());
        String inputMiddleName = String.format(inputText, "middleName", employee.getMiddleName());
        Integer departemntId = employee.getDepartmentId();

        select.setNullAllowed(false);
        select.setSelectName("departmentId");
        select.setSelectedId(departemntId);
        select.setRemotes(departmentHome.findByRange(-1, 0));
        String departmentSelect = select.generate();

        String rowsTextFilled = String.format(rowsText, id, inputLastName, inputFirstName, inputMiddleName);
        String rowsSelectFilled = String.format(rowsSelect, departmentSelect);
        String tableFilled = String.format(table, rowsTextFilled + rowsSelectFilled);


        String links = (isUpdate)
                ? String.format(linksDiv, linkTable + lD + linkRetrieve + lD + linkCopy + lD + linkDelete)
                : String.format(linksDiv, linkTable + lD + linkRetrieve + lD + linkUpdate + lD + linkDelete);
        links = String.format(links, id);

        return String.format(form, tableFilled + inputSubmit) + links;
    }

    public String genCreate() throws FinderException, RemoteException
    {
        String inputHiddenParam = String.format(inputHidden, "create", "create");
        String inputLastName = String.format(inputText, "lastName", "");
        String inputFirstName = String.format(inputText, "firstName", "");
        String inputMiddleName = String.format(inputText, "middleName", "");
        String rowsTextFilled = String.format(rowsText, "", inputLastName, inputFirstName, inputMiddleName);

        select.setNullAllowed(false);
        select.setSelectName("departmentId");
        select.setRemotes(departmentHome.findByRange(-1, 0));
        String departmentSelect = select.generate();

        String rowsSelectFilled = String.format(rowsSelect, departmentSelect);
        String tableFilled = String.format(table, rowsTextFilled + rowsSelectFilled);

        return String.format(form, inputHiddenParam + tableFilled + inputSubmit);
    }

    public String genRetrieve() throws RemoteException, FinderException
    {
        Employee employee = bean.getData();

        Integer id = employee.getId();
        String lastName = employee.getLastName();
        String firstName = employee.getFirstName();
        String middleName = employee.getMiddleName();
        Integer departmentId = employee.getDepartmentId();

        String departmentLinkCanvas = "<a href=\"../department?id=%s&act=retrieve\">%s</a>";
        String departmentLink = (NullUtil.isPositiveInteger(departmentId))
                ? String.format(departmentLinkCanvas,
                                departmentId,
                                departmentHome.findByPrimaryKey(departmentId).getData().getName())
                : Locale.NULL_READABLE;

        String rowsTextFilled = String.format(rowsText, id, lastName, firstName, middleName);
        String rowsSelectFilled = String.format(rowsSelect, departmentLink);
        String links = String.format(linksDiv, linkTable + lD + linkUpdate + lD + linkCopy + lD + linkDelete);
        links = String.format(links, id);

        return String.format(table, rowsTextFilled + rowsSelectFilled) + links;
    }

    public String genDelete() throws RemoteException
    {
        Employee employee = bean.getData();
        Integer id = employee.getId();
        String lastName = employee.getLastName();
        String firstName = employee.getFirstName();
        String middleName = employee.getMiddleName();
        String rowsTextFilled = String.format(rowsText, id, lastName, firstName, middleName);
        String tableFilled = String.format(table, rowsTextFilled);
        String links = String.format(linksDiv, linkTable + lD + linkRetrieve + lD + linkUpdate + lD + linkCopy);
        links = String.format(links, id);
        return String.format(form, tableFilled + inputSubmit) + links;
    }
}
